<?php

namespace Admin\Controller;
use Think\Controller;

//加载GatewayClient
require_once SITE_PATH.'/vendor/workerman/GatewayClient/Gateway.php';
use GatewayClient\Gateway;

class RobotController extends BaseController{
	
	public function run(){
		if(IS_POST){
			$mess = $this->robot_message();
			$robot = $this->robot();
			$state = F('state');
			$new_message = array(
				'type' => 'say',
				'head_img_url'=>'/Uploads'.$robot[0]['headimgurl'],
				'from_client_name' => $robot[0]['nickname'],
				'content'=>$mess[0]['content'],
				'time'=>date('H:i:s')
			);
			if($state==1){
				$new_message['room'] = 1;
				M('message')->add($new_message);
				$this->send_message_togroup(1,json_encode($new_message));
			}
		}else{
			$this->display();
		}
	}
	
	public function robot_message(){
		if($_SESSION['admin']['role_id']==0){
			$map['aid'] = 0;
		}else{
			$map['aid'] = $_SESSION['admin']['id'];
		}
		$count = M('robot_message')->where($map)->count();
     	$rand = mt_rand(0,$count-1); //产生随机数。
     	$limit = $rand.','.'1'; 
    	$data = M('robot_message')->where($map)->limit($limit)->select();  
		return $data;
	}
	
	public function robot(){
		if($_SESSION['admin']['role_id']==0){
			$map['aid'] = 0;
		}else{
			$map['aid'] = $_SESSION['admin']['id'];
		}
		$count = M('robot')->where($map)->count();
     	$rand = mt_rand(0,$count-1); //产生随机数。
     	$limit = $rand.','.'1'; 
    	$data = M('robot')->where($map)->limit($limit)->select();  
		return $data;
	}
	
	protected function send_message_togroup($group,$message){
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 向任意群组的网站页面发送数据
		Gateway::sendToGroup($group, $message);
	}
	
	protected function send_message_toall($message,$client_id_array=null ,$exclude_client_id){
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 向任意群组的网站页面发送数据
		Gateway::sendToAll($message,$client_id_array=null ,Gateway::getClientIdByUid($exclude_client_id));
	}
	
	public function integral(){
		$id = I('id');
		$info = M('robot')->where("id = $id")->find();
		$data['type'] = 'robot';
		$data['time'] = time();
		$data['content'] = '玩家['.$info['nickname'].']上分已受理，请刷新页面查看点数';
		$this->send_message_toall(json_encode($data));
		$this->success('成功');
	}
	public function under(){
		$id = I('id');
		$info = M('robot')->where("id = $id")->find();
		$data['type'] = 'robot';
		$data['time'] = time();
		$data['content'] = '玩家['.$info['nickname'].']回分已受理，请确认';
		$this->send_message_toall(json_encode($data));
		$this->success('成功');
	}
	
	public function index(){
		$nickname = I('nickname');
		$robot = M('robot');
		if($nickname){
			$count = $robot->where("nickname like '%{$nickname}%'")->count();
			$page = new \Think\Page($count,10);
			$show = $page->show();
			$list = $robot->where("nickname like '%{$nickname}%'")->limit($page->firstRow.','.$page->listRows)->order("id ASC")->select();
		}else{
			$count = $robot->count();
			$page = new \Think\Page($count,10);
			$show = $page->show();
			$list = $robot->limit($page->firstRow.','.$page->listRows)->order("id ASC")->select();
		}
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display();
	}
	
	public function edit(){
		$id = I('id');
		$robot = M('robot');
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$file = I('file0');//获取图片路径
                $checkpic = I('checkpic');
                $oldcheckpic = I('oldcheckpic');
				
                if ($checkpic != $oldcheckpic) {
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize = 3145728;// 设置附件上传大小
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath = './Uploads/carousel/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $info = $upload->upload();

                    if ($info) {
                        $img_url = '/carousel/' . $info[file0][savepath] . $info[file0][savename];//如果上传成功则完成路径拼接
                    } else {
                        $this->error($upload->getError());//否则就是上传错误，显示错误原因
                    }
                }
				if ($checkpic != $oldcheckpic) {
                    $data['headimgurl'] = $img_url;
                }
				$data['nickname'] = I('nickname');
				
				if($data){
					$res = $robot->where("id = $id")->save($data);
					if($res){
						$this->success('编辑成功！',U('Admin/Robot/index'),1);
					}else{
						$this->error('编辑失败');
					}
				}
			}
		}else{
			$info = $robot->where("id = $id")->find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	
	public function add(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$file = I('file0');//获取图片路径
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Uploads/carousel/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->saveRule = 'time';
                $info = $upload->upload();

                if ($info) {
                    $img_url = '/carousel/' . $info[file0][savepath] . $info[file0][savename];//如果上传成功则完成路径拼接

                } elseif (!$file) {
                    $img_url = '';//否则如果字段为空，表示没有上传任何文件，赋值空
				 
              	} else {
                  	$this->error($upload->getError());//否则就是上传错误，显示错误原因
              	}
				
				$data['nickname'] = I('nickname');
				$data['headimgurl'] = $img_url;
				if($data){
					$res = M('robot')->add($data);
					if($res){
						$this->success('添加成功!',U('Admin/Robot/add'),1);
					}else{
						$this->error('添加失败');
					}
				}
			}
		}else{
			$this->display();
		}
	}
	
	public function del(){
		$id = I('id');
		if($id){
			$res = M('robot')->where("id = $id")->delete();
			if($res){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}
	}
	
	
	public function message(){
		$message = M('robot_message');
		$count = $message->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $message->limit($page->firstRow.','.$page->listRows)->order("id ASC")->select();
		
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display();
	}
	
	public function add_message(){
		$message = M('robot_message');	
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$data['content'] = I('content');
				if(empty($data['content'])){
					$this->error('请输入竞猜内容');
				}
				$res = $message->add($data);
				if($res){
					$this->success('添加成功',U('Admin/Robot/message'),1);
				}else{
					$this->error('添加失败');
				}
			}
		}else{
			$this->display();
		}
	}
	public function edit_message(){
		$id = I('id');
		$message = M('robot_message');	
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$data['content'] = I('content');
				if(empty($data['content'])){
					$this->error('请输入竞猜内容');
				}
				$res = $message->where("id = $id")->save($data);
				if($res){
					$this->success('修改成功',U('Admin/Robot/message'),1);
				}else{
					$this->error('修改失败');
				}
			}
		}else{
			$info = $message->where("id = $id")->find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	public function del_message(){
		$id = I('id');
		$res = M('robot_message')->where("id = $id")->delete();
		$this->success('删除成功');
	}
	
	public function kefu(){
		$admin = session('admin');
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$file = I('file0');//获取图片路径
                $checkpic = I('checkpic');
                $oldcheckpic = I('oldcheckpic');
				
                if ($checkpic != $oldcheckpic) {
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize = 3145728;// 设置附件上传大小
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath = './Uploads/carousel/kefu/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $info = $upload->upload();

                    if ($info) {
                        $img_url = '/carousel/kefu/' . $info[file0][savepath] . $info[file0][savename];//如果上传成功则完成路径拼接
                    } else {
                        $this->error($upload->getError());//否则就是上传错误，显示错误原因
                    }
                }
				 
				if ($checkpic != $oldcheckpic) {
                    $data['kefu'] = $img_url;
					$data['id'] = 1;
                }
				if($data['kefu']){
					$result = M('config')->save($data);
				} 
				if($result){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}
		}else{
			$info = M('config')->where("id = 1")->find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	
	public function card(){
		$admin = session('admin');
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$file = I('file0');//获取图片路径
                $checkpic = I('checkpic');
                $oldcheckpic = I('oldcheckpic');
				
                if ($checkpic != $oldcheckpic) {
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize = 3145728;// 设置附件上传大小
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath = './Uploads/carousel/kefu/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $info = $upload->upload();

                    if ($info) {
                        $img_url = '/carousel/kefu/' . $info[file0][savepath] . $info[file0][savename];//如果上传成功则完成路径拼接
                    } else {
                        $this->error($upload->getError());//否则就是上传错误，显示错误原因
                    }
                }
				 
				if ($checkpic != $oldcheckpic) {
                    $data['card'] = $img_url;
					$data['id'] = 1;
                }
				if($data['card']){
					$result = M('config')->save($data);
				} 
				if($result){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}
		}else{
			$info = M('config')->where("id = 1")->find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	
	public function wx_sk(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$file = I('file0');//获取图片路径
                $checkpic = I('checkpic');
                $oldcheckpic = I('oldcheckpic');
				
                if ($checkpic != $oldcheckpic) {
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize = 3145728;// 设置附件上传大小
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath = './Uploads/carousel/kefu/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $info = $upload->upload();

                    if ($info) {
                        $img_url = '/carousel/kefu/' . $info[file0][savepath] . $info[file0][savename];//如果上传成功则完成路径拼接
                    } else {
                        $this->error($upload->getError());//否则就是上传错误，显示错误原因
                    }
                }
				 
				if ($checkpic != $oldcheckpic) {
                    $data['wx'] = $img_url;
					$data['id'] = 1;
                }
				if($data['wx']){
					$result = M('config')->save($data);
				}
				if($result){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}
		}else{
			$info = M('config')->where("id = 1")->find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	
	
	public function alipay(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$file = I('file0');//获取图片路径
                $checkpic = I('checkpic');
                $oldcheckpic = I('oldcheckpic');
				
                if ($checkpic != $oldcheckpic) {
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize = 3145728;// 设置附件上传大小
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath = './Uploads/carousel/kefu/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $info = $upload->upload();

                    if ($info) {
                        $img_url = '/carousel/kefu/' . $info[file0][savepath] . $info[file0][savename];//如果上传成功则完成路径拼接
                    } else {
                        $this->error($upload->getError());//否则就是上传错误，显示错误原因
                    }
                }
				 
				if ($checkpic != $oldcheckpic) {
                    $data['alipay'] = $img_url;
					$data['id'] = 1;
                }
				if($data['alipay']){
					$result = M('config')->save($data);
				}
				if($result){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}
		}else{
			$info = M('config')->where("id = 1")->find();
			$this->assign('info',$info);
			$this->display();
		}
	}
	
	
}

?>